

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>optuna.pruners._hyperband &mdash; Optuna 1.4.0 文档</title>
  

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/css/custom.css" type="text/css" />
    <link rel="index" title="索引" href="../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> Optuna
          

          
          </a>

          
            
            
              <div class="version">
                1.4.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">安装</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorial/index.html">教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../faq.html">常见问题</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">Optuna</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">模块代码</a> &raquo;</li>
        
      <li>optuna.pruners._hyperband</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>optuna.pruners._hyperband 源代码</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">math</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Union</span>

<span class="kn">import</span> <span class="nn">optuna</span>
<span class="kn">from</span> <span class="nn">optuna._experimental</span> <span class="kn">import</span> <span class="n">experimental</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">logging</span>
<span class="kn">from</span> <span class="nn">optuna.pruners._base</span> <span class="kn">import</span> <span class="n">BasePruner</span>
<span class="kn">from</span> <span class="nn">optuna.pruners._successive_halving</span> <span class="kn">import</span> <span class="n">SuccessiveHalvingPruner</span>
<span class="kn">from</span> <span class="nn">optuna.trial._state</span> <span class="kn">import</span> <span class="n">TrialState</span>

<span class="n">_logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">get_logger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>


<div class="viewcode-block" id="HyperbandPruner"><a class="viewcode-back" href="../../../reference/pruners.html#optuna.pruners.HyperbandPruner">[文档]</a><span class="nd">@experimental</span><span class="p">(</span><span class="s2">&quot;1.1.0&quot;</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">HyperbandPruner</span><span class="p">(</span><span class="n">BasePruner</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Pruner using Hyperband.</span>

<span class="sd">    As SuccessiveHalving (SHA) requires the number of configurations</span>
<span class="sd">    :math:`n` as its hyperparameter.  For a given finite budget :math:`B`,</span>
<span class="sd">    all the configurations have the resources of :math:`B \\over n` on average.</span>
<span class="sd">    As you can see, there will be a trade-off of :math:`B` and :math:`B \\over n`.</span>
<span class="sd">    `Hyperband &lt;http://www.jmlr.org/papers/volume18/16-558/16-558.pdf&gt;`_ attacks this trade-off</span>
<span class="sd">    by trying different :math:`n` values for a fixed budget.</span>

<span class="sd">    .. note::</span>
<span class="sd">        * In the Hyperband paper, the counterpart of :class:`~optuna.samplers.RandomSampler`</span>
<span class="sd">          is used.</span>
<span class="sd">        * Optuna uses :class:`~optuna.samplers.TPESampler` by default.</span>
<span class="sd">        * `The benchmark result</span>
<span class="sd">          &lt;https://github.com/optuna/optuna/pull/828#issuecomment-575457360&gt;`_</span>
<span class="sd">          shows that :class:`optuna.pruners.HyperbandPruner` supports both samplers.</span>

<span class="sd">    .. note::</span>
<span class="sd">        If you use ``HyperbandPruner`` with :class:`~optuna.samplers.TPESampler`,</span>
<span class="sd">        it&#39;s recommended to consider to set larger ``n_trials`` or ``timeout`` to make full use of</span>
<span class="sd">        the characteristics of :class:`~optuna.samplers.TPESampler`</span>
<span class="sd">        because :class:`~optuna.samplers.TPESampler` uses some (by default, :math:`10`)</span>
<span class="sd">        :class:`~optuna.trial.Trial`\\ s for its startup.</span>

<span class="sd">        As Hyperband runs multiple :class:`~optuna.pruners.SuccessiveHalvingPruner` and collect</span>
<span class="sd">        trials based on the current :class:`~optuna.trial.Trial`\\ &#39;s bracket ID, each bracket</span>
<span class="sd">        needs to observe more than :math:`10` :class:`~optuna.trial.Trial`\\ s</span>
<span class="sd">        for :class:`~optuna.samplers.TPESampler` to adapt its search space.</span>

<span class="sd">        Thus, for example, if ``HyperbandPruner`` has :math:`4` pruners in it,</span>
<span class="sd">        at least :math:`4 \\times 10` trials are consumed for startup.</span>

<span class="sd">    .. note::</span>
<span class="sd">        Hyperband has several :class:`~optuna.pruners.SuccessiveHalvingPruner`. Each</span>
<span class="sd">        :class:`~optuna.pruners.SuccessiveHalvingPruner` is referred as &quot;bracket&quot; in the original</span>
<span class="sd">        paper. The number of brackets is an important factor to control the early stopping behavior</span>
<span class="sd">        of Hyperband and is automatically determined by ``min_resource``, ``max_resource`` and</span>
<span class="sd">        ``reduction_factor`` as</span>
<span class="sd">        `The number of brackets = floor(log_{reduction_factor}(max_resource / min_resource)) + 1`.</span>
<span class="sd">        Please set ``reduction_factor`` so that the number of brackets is not too large　(about 4 ~</span>
<span class="sd">        6 in most use cases).　Please see Section 3.6 of the `original paper</span>
<span class="sd">        &lt;http://www.jmlr.org/papers/volume18/16-558/16-558.pdf&gt;`_ for the detail.</span>

<span class="sd">    Example:</span>

<span class="sd">        We minimize an objective function with Hyperband pruning algorithm.</span>

<span class="sd">        .. testcode::</span>

<span class="sd">            import numpy as np</span>
<span class="sd">            from sklearn.datasets import load_iris</span>
<span class="sd">            from sklearn.linear_model import SGDClassifier</span>
<span class="sd">            from sklearn.model_selection import train_test_split</span>

<span class="sd">            import optuna</span>

<span class="sd">            X, y = load_iris(return_X_y=True)</span>
<span class="sd">            X_train, X_test, y_train, y_test = train_test_split(X, y)</span>
<span class="sd">            classes = np.unique(y)</span>
<span class="sd">            n_train_iter = 100</span>

<span class="sd">            def objective(trial):</span>
<span class="sd">                alpha = trial.suggest_uniform(&#39;alpha&#39;, 0.0, 1.0)</span>
<span class="sd">                clf = SGDClassifier(alpha=alpha)</span>

<span class="sd">                for step in range(n_train_iter):</span>
<span class="sd">                    clf.partial_fit(X_train, y_train, classes=classes)</span>

<span class="sd">                    intermediate_value = clf.score(X_valid, y_valid)</span>
<span class="sd">                    trial.report(intermediate_value, step)</span>

<span class="sd">                    if trial.should_prune():</span>
<span class="sd">                        raise optuna.TrialPruned()</span>

<span class="sd">                return clf.score(X_valid, y_valid)</span>

<span class="sd">            study = optuna.create_study(</span>
<span class="sd">                direction=&#39;maximize&#39;,</span>
<span class="sd">                pruner=optuna.pruners.HyperbandPruner(</span>
<span class="sd">                    min_resource=1,</span>
<span class="sd">                    max_resource=n_train_iter,</span>
<span class="sd">                    reduction_factor=3</span>
<span class="sd">                )</span>
<span class="sd">            )</span>
<span class="sd">            study.optimize(objective, n_trials=20)</span>

<span class="sd">    Args:</span>
<span class="sd">        min_resource:</span>
<span class="sd">            A parameter for specifying the minimum resource allocated to a trial noted as :math:`r`</span>
<span class="sd">            in the paper. A smaller :math:`r` will give a result faster, but a larger</span>
<span class="sd">            :math:`r` will give a better guarantee of successful judging between configurations.</span>
<span class="sd">            See the details for :class:`~optuna.pruners.SuccessiveHalvingPruner`.</span>
<span class="sd">        max_resource:</span>
<span class="sd">            A parameter for specifying the maximum resource allocated to a trial. :math:`R` in the</span>
<span class="sd">            paper corresponds to ``max_resource / min_resource``. This value represents and should</span>
<span class="sd">            match the maximum iteration steps (e.g., the number of epochs for neural networks).</span>
<span class="sd">            When this argument is &quot;auto&quot;, the maximum resource is estimated according to the</span>
<span class="sd">            completed trials. The default value of this argument is &quot;auto&quot;.</span>

<span class="sd">            .. note::</span>
<span class="sd">                With &quot;auto&quot;, the maximum resource will be the largest step reported by</span>
<span class="sd">                :meth:`~optuna.trial.Trial.report` in the first, or one of the first if trained in</span>
<span class="sd">                parallel, completed trial. No trials will be pruned until the maximum resource is</span>
<span class="sd">                determined.</span>

<span class="sd">            .. note::</span>
<span class="sd">                If the step of the last intermediate value may change with each trial, please</span>
<span class="sd">                manually specify the maximum possible step to ``max_resource``.</span>
<span class="sd">        reduction_factor:</span>
<span class="sd">            A parameter for specifying reduction factor of promotable trials noted as</span>
<span class="sd">            :math:`\\eta` in the paper.</span>
<span class="sd">            See the details for :class:`~optuna.pruners.SuccessiveHalvingPruner`.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">min_resource</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span>
        <span class="n">max_resource</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;auto&quot;</span><span class="p">,</span>
        <span class="n">reduction_factor</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">3</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_min_resource</span> <span class="o">=</span> <span class="n">min_resource</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_max_resource</span> <span class="o">=</span> <span class="n">max_resource</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reduction_factor</span> <span class="o">=</span> <span class="n">reduction_factor</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_pruners</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c1"># type: List[SuccessiveHalvingPruner]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_total_trial_allocation_budget</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_trial_allocation_budgets</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c1"># type: List[int]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_n_brackets</span> <span class="o">=</span> <span class="kc">None</span>  <span class="c1"># type: Optional[int]</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_max_resource</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_max_resource</span> <span class="o">!=</span> <span class="s2">&quot;auto&quot;</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The &#39;max_resource&#39; should be integer or &#39;auto&#39;. &quot;</span>
                <span class="s2">&quot;But max_resource = </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_max_resource</span><span class="p">)</span>
            <span class="p">)</span>

    <span class="k">def</span> <span class="nf">prune</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">:</span> <span class="s2">&quot;optuna.study.Study&quot;</span><span class="p">,</span> <span class="n">trial</span><span class="p">:</span> <span class="s2">&quot;optuna.trial.FrozenTrial&quot;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pruners</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_try_initialization</span><span class="p">(</span><span class="n">study</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pruners</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">False</span>

        <span class="n">bracket_id</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_bracket_id</span><span class="p">(</span><span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">)</span>
        <span class="n">_logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2">th bracket is selected&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">bracket_id</span><span class="p">))</span>
        <span class="n">bracket_study</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_bracket_study</span><span class="p">(</span><span class="n">study</span><span class="p">,</span> <span class="n">bracket_id</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pruners</span><span class="p">[</span><span class="n">bracket_id</span><span class="p">]</span><span class="o">.</span><span class="n">prune</span><span class="p">(</span><span class="n">bracket_study</span><span class="p">,</span> <span class="n">trial</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_try_initialization</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">:</span> <span class="s2">&quot;optuna.study.Study&quot;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_max_resource</span> <span class="o">==</span> <span class="s2">&quot;auto&quot;</span><span class="p">:</span>
            <span class="n">trials</span> <span class="o">=</span> <span class="n">study</span><span class="o">.</span><span class="n">get_trials</span><span class="p">(</span><span class="n">deepcopy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
            <span class="n">n_steps</span> <span class="o">=</span> <span class="p">[</span>
                <span class="n">t</span><span class="o">.</span><span class="n">last_step</span>
                <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">trials</span>
                <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">state</span> <span class="o">==</span> <span class="n">TrialState</span><span class="o">.</span><span class="n">COMPLETE</span> <span class="ow">and</span> <span class="n">t</span><span class="o">.</span><span class="n">last_step</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
            <span class="p">]</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="n">n_steps</span><span class="p">:</span>
                <span class="k">return</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">_max_resource</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">n_steps</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>

        <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_max_resource</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_n_brackets</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># In the original paper http://www.jmlr.org/papers/volume18/16-558/16-558.pdf, the</span>
            <span class="c1"># inputs of Hyperband are `R`: max resource and `\eta`: reduction factor. The</span>
            <span class="c1"># number of brackets (this is referred as `s_{max} + 1` in the paper) is calculated</span>
            <span class="c1"># by s_{max} + 1 = \floor{\log_{\eta} (R)} + 1 in Algorithm 1 of the original paper.</span>
            <span class="c1"># In this implementation, we combine this formula and that of ASHA paper</span>
            <span class="c1"># https://arxiv.org/abs/1502.07943 as</span>
            <span class="c1"># `n_brackets = floor(log_{reduction_factor}(max_resource / min_resource)) + 1`</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_n_brackets</span> <span class="o">=</span> <span class="p">(</span>
                <span class="n">math</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span>
                    <span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_max_resource</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">_min_resource</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_reduction_factor</span><span class="p">)</span>
                <span class="p">)</span>
                <span class="o">+</span> <span class="mi">1</span>
            <span class="p">)</span>

        <span class="n">_logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;Hyperband has </span><span class="si">{}</span><span class="s2"> brackets&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_n_brackets</span><span class="p">))</span>

        <span class="k">for</span> <span class="n">bracket_id</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_n_brackets</span><span class="p">):</span>
            <span class="n">trial_allocation_budget</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_calculate_trial_allocation_budget</span><span class="p">(</span><span class="n">bracket_id</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_total_trial_allocation_budget</span> <span class="o">+=</span> <span class="n">trial_allocation_budget</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_trial_allocation_budgets</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">trial_allocation_budget</span><span class="p">)</span>

            <span class="n">pruner</span> <span class="o">=</span> <span class="n">SuccessiveHalvingPruner</span><span class="p">(</span>
                <span class="n">min_resource</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_min_resource</span><span class="p">,</span>
                <span class="n">reduction_factor</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_reduction_factor</span><span class="p">,</span>
                <span class="n">min_early_stopping_rate</span><span class="o">=</span><span class="n">bracket_id</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_pruners</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pruner</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_calculate_trial_allocation_budget</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bracket_id</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Compute the trial allocated budget for a bracket of ``bracket_id``.</span>

<span class="sd">        In the `original paper &lt;http://www.jmlr.org/papers/volume18/16-558/16-558.pdf&gt;`, the</span>
<span class="sd">        number of trials per one bracket is referred as ``n`` in Algorithm 1. Since we do not know</span>
<span class="sd">        the total number of trials in the leaning scheme of Optuna, we calculate the ratio of the</span>
<span class="sd">        number of trials here instead.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">_n_brackets</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
        <span class="n">s</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_n_brackets</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">bracket_id</span>
        <span class="k">return</span> <span class="n">math</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_n_brackets</span> <span class="o">*</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_reduction_factor</span> <span class="o">**</span> <span class="n">s</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">s</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">_get_bracket_id</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">:</span> <span class="s2">&quot;optuna.study.Study&quot;</span><span class="p">,</span> <span class="n">trial</span><span class="p">:</span> <span class="s2">&quot;optuna.trial.FrozenTrial&quot;</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Compute the index of bracket for a trial of ``trial_number``.</span>

<span class="sd">        The index of a bracket is noted as :math:`s` in</span>
<span class="sd">        `Hyperband paper &lt;http://www.jmlr.org/papers/volume18/16-558/16-558.pdf&gt;`_.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pruners</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="mi">0</span>

        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">_n_brackets</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
        <span class="n">n</span> <span class="o">=</span> <span class="p">(</span>
            <span class="nb">hash</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2">_</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study</span><span class="o">.</span><span class="n">study_name</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">number</span><span class="p">))</span>
            <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_total_trial_allocation_budget</span>
        <span class="p">)</span>
        <span class="k">for</span> <span class="n">bracket_id</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_n_brackets</span><span class="p">):</span>
            <span class="n">n</span> <span class="o">-=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_trial_allocation_budgets</span><span class="p">[</span><span class="n">bracket_id</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">bracket_id</span>

        <span class="k">assert</span> <span class="kc">False</span><span class="p">,</span> <span class="s2">&quot;This line should be unreachable.&quot;</span>

    <span class="k">def</span> <span class="nf">_create_bracket_study</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">:</span> <span class="s2">&quot;optuna.study.Study&quot;</span><span class="p">,</span> <span class="n">bracket_id</span><span class="p">:</span> <span class="nb">int</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;optuna.study.Study&quot;</span><span class="p">:</span>
        <span class="c1"># This class is assumed to be passed to</span>
        <span class="c1"># `SuccessiveHalvingPruner.prune` in which `get_trials`,</span>
        <span class="c1"># `direction`, and `storage` are used.</span>
        <span class="c1"># But for safety, prohibit the other attributes explicitly.</span>
        <span class="k">class</span> <span class="nc">_BracketStudy</span><span class="p">(</span><span class="n">optuna</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">Study</span><span class="p">):</span>

            <span class="n">_VALID_ATTRS</span> <span class="o">=</span> <span class="p">(</span>
                <span class="s2">&quot;get_trials&quot;</span><span class="p">,</span>
                <span class="s2">&quot;direction&quot;</span><span class="p">,</span>
                <span class="s2">&quot;_storage&quot;</span><span class="p">,</span>
                <span class="s2">&quot;_study_id&quot;</span><span class="p">,</span>
                <span class="s2">&quot;pruner&quot;</span><span class="p">,</span>
                <span class="s2">&quot;study_name&quot;</span><span class="p">,</span>
                <span class="s2">&quot;_bracket_id&quot;</span><span class="p">,</span>
                <span class="s2">&quot;sampler&quot;</span><span class="p">,</span>
                <span class="s2">&quot;trials&quot;</span><span class="p">,</span>
            <span class="p">)</span>

            <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">:</span> <span class="s2">&quot;optuna.study.Study&quot;</span><span class="p">,</span> <span class="n">bracket_id</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
                <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
                    <span class="n">study_name</span><span class="o">=</span><span class="n">study</span><span class="o">.</span><span class="n">study_name</span><span class="p">,</span>
                    <span class="n">storage</span><span class="o">=</span><span class="n">study</span><span class="o">.</span><span class="n">_storage</span><span class="p">,</span>
                    <span class="n">sampler</span><span class="o">=</span><span class="n">study</span><span class="o">.</span><span class="n">sampler</span><span class="p">,</span>
                    <span class="n">pruner</span><span class="o">=</span><span class="n">study</span><span class="o">.</span><span class="n">pruner</span><span class="p">,</span>
                <span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_bracket_id</span> <span class="o">=</span> <span class="n">bracket_id</span>

            <span class="k">def</span> <span class="nf">get_trials</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">deepcopy</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="s2">&quot;optuna.trial.FrozenTrial&quot;</span><span class="p">]:</span>
                <span class="n">trials</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_trials</span><span class="p">(</span><span class="n">deepcopy</span><span class="o">=</span><span class="n">deepcopy</span><span class="p">)</span>
                <span class="n">pruner</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pruner</span>
                <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">pruner</span><span class="p">,</span> <span class="n">HyperbandPruner</span><span class="p">)</span>
                <span class="k">return</span> <span class="p">[</span><span class="n">t</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">trials</span> <span class="k">if</span> <span class="n">pruner</span><span class="o">.</span><span class="n">_get_bracket_id</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_bracket_id</span><span class="p">]</span>

            <span class="k">def</span> <span class="fm">__getattribute__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr_name</span><span class="p">):</span>  <span class="c1"># type: ignore</span>
                <span class="k">if</span> <span class="n">attr_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">_BracketStudy</span><span class="o">.</span><span class="n">_VALID_ATTRS</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span>
                        <span class="s2">&quot;_BracketStudy does not have attribute of &#39;</span><span class="si">{}</span><span class="s2">&#39;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">attr_name</span><span class="p">)</span>
                    <span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">return</span> <span class="nb">object</span><span class="o">.</span><span class="fm">__getattribute__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr_name</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">_BracketStudy</span><span class="p">(</span><span class="n">study</span><span class="p">,</span> <span class="n">bracket_id</span><span class="p">)</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Optuna Contributors.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
    <a href="../../../privacy.html">Privacy Policy</a>.
     


</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-55135190-8', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>